package com.fancy.social.service.impl;

import com.fancy.social.dto.CommentCreateDTO;
import com.fancy.social.entity.Comment;
import com.fancy.social.mapper.CommentMapper;
import com.fancy.social.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

@Service
public class CommentServiceImpl implements CommentService {

    @Autowired
    private CommentMapper commentMapper;

    @Override
    public void createComment(Long userId, CommentCreateDTO commentCreateDTO) {
        Comment comment = new Comment();
        comment.setUserId(userId);
        comment.setTargetId(commentCreateDTO.getTargetId());
        comment.setTargetType(commentCreateDTO.getTargetType());
        comment.setContent(commentCreateDTO.getContent());
        comment.setParentId(commentCreateDTO.getParentId() != null ? commentCreateDTO.getParentId() : 0L);
        comment.setCreateTime(LocalDateTime.now());
        comment.setLikeCount(0);
        comment.setReplyCount(0);
        comment.setStatus(1);
        commentMapper.insert(comment);
    }

    @Override
    public void deleteComment(Long userId, Long commentId) {
        Comment comment = commentMapper.selectById(commentId);
        if (comment != null && comment.getUserId().equals(userId)) {
            commentMapper.deleteById(commentId);
        }
    }
}

